<?php

namespace App\Http\Controllers\Admin;

use App\Model\Role;
use App\Model\AuthNode;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route;

class RoleController extends Controller
{

    private function leftBar() {
        return [
            'title' => '管理员管理',
            'list'  => [
                [
                    'title' => '管理员列表',
                    'type' => 'userlist',
                    'url' => url('admin/user'),
                ],
                [
                    'title' => '添加管理员',
                    'type' => 'adduser',
                    'url' => url('admin/user/create'),
                ],
                [
                    'title' => '角色列表',
                    'type' => 'rolelist',
                    'url' => url('admin/role'),
                ],
                [
                    'title' => '添加角色',
                    'type' => 'addrole',
                    'url' => url('admin/role/create'),
                ],
                [
                    'title' => '节点列表',
                    'type' => 'nodelist',
                    'url' => url('admin/node'),
                ],
                [
                    'title' => '添加节点',
                    'type' => 'addnode',
                    'url' => url('admin/node/create'),
                ],
            ]

        ];
    }

    public function __construct(Request $request){
        $this->request = $request;
        $this->limit = config('config.pageLimit');
        $leftBarArr = $this->leftBar();
        view()->share(['title' => $leftBarArr['title'],'list'=>$leftBarArr['list'],'limit'=>$this->limit]);
    }

    /**
     * Display a listing of the resource.
     * 角色列表页
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $keyWord = $this->request->input('keyword');
        $res = Role::getList($keyWord,$this->limit)->getData();
        $this->request->flash();
        return view('admin/role/index')
            ->with('res',$res)
            ->with('titleType','rolelist');
    }

    /**
     * Show the form for creating a new resource.
     * 添加保存角色
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        if($this->request->isMethod('put')) {
            $input = $this->request->except('_token');
			$title = Role::where("title",$input['title'])->pluck('title');
            if ($title!='') {
            	echo "<script>alert('角色已经存在');</script>";
				echo "<script>history.go(-1);</script>";
				
            }else{
				 $input['node_id'] = implode(',', $input['node_id']);
				Role::create($input);
				return redirect('admin/role');
			}
           
        } else {
            $parentNode = AuthNode::getLists()->getData();
            return view('admin/role/create')
                ->with('parentNode',$parentNode)
                ->with('titleType','addrole');
        }
    }

    /**
     * Show the form for editing the specified resource.
     * 编辑保存角色
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        if($this->request->isMethod('put')) {
            $input = $this->request->except('_method','_token');
            $input['node_id'] = implode(',', $input['node_id']);
            $res = Role::updateData($input, $id)->getData();
            if ($res) {
                return redirect('admin/role');
            }
        } else {
            $one = Role::getOne($id)->getData();
            $parentNode = AuthNode::getLists()->getData();
            return view('admin.role.edit')
                ->with('one', $one)
                ->with('parentNode', $parentNode);
        }
    }

    /**
     * Remove the specified resource from storage.
     * 删除角色
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        $del = Role::deleteRole($id);
        return $del->getResult();
    }

    /**
     * @param $id
     * 禁用、启用
     */
    public function able($id,$able)
    {
        $res = Role::able($id,$able);
        return $res->getResult();
    }
}
